﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using MeizuSDK.Presentation;
using System.Drawing;
using MeizuSDK.Drawing;
using System.Diagnostics;
using System.Threading;

namespace Demo
{
    class ListWindow : DemoWindowBase
    {
        ListBox list = new ListBox();
        AlphabetBar alphabetBar = new AlphabetBar();

        private ImageContainer imgContainer;
        private int DefaultGridMenuId = 2000;

        private ImagingHelper img1;
        private ImagingHelper img1Pressed;
        private ImagingHelper img2;
        private ImagingHelper img2Pressed;
        private ImagingHelper img3;
        private ImagingHelper img3Pressed;
        private ImagingHelper img4;
        private ImagingHelper img4Pressed;

        private ImagingHelper imgOption;
        private ImagingHelper imgOptionPressed;
        private GridMenu menu;

        public ListWindow()
        {
            imgContainer = new ImageContainer();

            img1 = imgContainer.LoadImageFromMzResV2(MzResV2.Png_Tick, true);
            img1Pressed = imgContainer.LoadImageFromMzResV2(MzResV2.Png_Tick_Pressed, true);
            img2 = imgContainer.LoadImageFromMzResV2(MzResV2.Png_Download, true);
            img2Pressed = imgContainer.LoadImageFromMzResV2(MzResV2.Png_Download_Pressed, true);
            img3 = imgContainer.LoadImageFromMzResV2(MzResV2.Png_Cancel, true);
            img3Pressed = imgContainer.LoadImageFromMzResV2(MzResV2.Png_Cancel_Pressed, true);
            img4 = imgContainer.LoadImageFromMzResV2(MzResV2.Png_Search, true);
            img4Pressed = imgContainer.LoadImageFromMzResV2(MzResV2.Png_Search_Pressed, true);

            imgOption = imgContainer.LoadImageFromMzResV2(MzResV2.Png_Read, true);
            imgOptionPressed = imgContainer.LoadImageFromMzResV2(MzResV2.Png_Read_Pressed, true);

            menu = new GridMenu();
            Controls.Add(menu);
            //menu.ItemClicked += new GridMenuItemClickedEventHandler(menu_ItemClicked);

            GridMenuItem mnuItem = new GridMenuItem(DefaultGridMenuId + 1, "滚到顶部", img1, img1Pressed);
            menu.Items.Add(mnuItem);
            mnuItem = new GridMenuItem(DefaultGridMenuId + 2, "滚到底部", img2, img2Pressed);
            menu.Items.Add(mnuItem);
            mnuItem = new GridMenuItem(DefaultGridMenuId + 3, "清空列表", img3, img3Pressed);
            menu.Items.Add(mnuItem);
            mnuItem = new GridMenuItem(DefaultGridMenuId + 4, "填充列表", img4, img4Pressed);
            menu.Items.Add(mnuItem);

            toolbar.SetIconButton(ToolBarButtonIndex.MiddleIconButton1, true, true, imgOption, imgOptionPressed, null);

            list.Location = new Point(0, 0);
            list.Size = new Size(Size.Width, Size.Height - ToolBar.HEIGHT);
            list.UltraGridLines = true;// 启用额外列表线
            list.OwnerDrawItem = true;// 启用自定义绘制
            list.DrawItem += new DrawItemEventHandler(list_DrawItem);
            list.ItemSelected += new ItemSelectedEventHandler(list_ItemSelected);

            Controls.Add(list);

            alphabetBar.Location = new Point(Width - 50, 0);
            alphabetBar.Height = (Height - ToolBar.HEIGHT);
            alphabetBar.EnableZoom = true;
            alphabetBar.CurrentLetterChanged += new EventHandler(alphabetBar_CurrentLetterChanged);

            Controls.Add(alphabetBar);

            ThreadStart method = new ThreadStart(FillList);
            Thread th = new Thread(method);
            th.Start();
        }
        /// <summary>
        /// 自定义绘制列表项
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void list_DrawItem(object sender, DrawItemEventArgs e)
        {
            if (list.IsLButtonDown && !list.IsMouseDownAtScrolling && e.ItemIndex == list.LastClickedIndex)
            {
                e.DrawItemSelectedBackground();
            }

            ListItem item = list.Items[e.ItemIndex];

            if (item != null)
            {
                Graphics g = e.Graphics;

                using (SolidBrush brush = new SolidBrush(ForeColor))
                {
                    StringFormat sf = new StringFormat();
                    sf.Alignment = StringAlignment.Near;// 文本靠左对齐
                    sf.LineAlignment = StringAlignment.Center;// 垂直居中

                    Rectangle textRect = e.ItemRectangle;// 获取文本内容所在矩形
                    textRect.Inflate(-10, -4);

                    g.DrawString(item.Text, list.Font, brush, textRect, sf);
                }
            }
        }

        void alphabetBar_CurrentLetterChanged(object sender, EventArgs e)
        {
            string result = "Current Letter:" + alphabetBar.CurrentLetter;

            Debug.WriteLine(result);
        }

        void list_ItemSelected(object sender, ItemSelectedEventArgs e)
        {
            if (e.Item != null)
            {
                MessageBox.Show(" 用户选择了第" + e.ItemIndex + "项【" + e.Item.Text + "】", "List");
            }
        }

        void list_Click(object sender, ListBoxClickEventArgs e)
        {
            MessageBox.Show(e.Index + " Clicked", "List");

        }

        protected override void toolbar_ButtonClick(object sender, ToolBar.ButtonEventArgs e)
        {
            base.toolbar_ButtonClick(sender, e);

            if (e.Index == ToolBarButtonIndex.MiddleIconButton1)
            {
                if (menu.IsContinue())
                {
                    menu.Close();
                }
                else
                {
                    menu.Show(HWnd, ToolBar.HEIGHT - 2);
                }

                return;
            }
        }

        protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
        {
            base.OnClosing(e);

            if(menu.IsContinue())
            {
                menu.Close();
            }
        }

        protected override void OnMzCommand(uint wParam, uint lParam)
        {
            int id = (int)NativeMethods.LOWORD(wParam);

            GridMenuItem item = menu.Items[id];

            if (item != null)
            {
                switch (item.ItemId)
                {
                    // 滚到顶部
                    case 2001:
                        list.ScrollToTop();
                        break;
                    // 滚到底部
                    case 2002:
                        list.ScrollToBottom();
                        break;
                    // 清空列表
                    case 2003:
                        list.Items.Clear();
                        break;
                    // 填充列表
                    case 2004:
                        FillList();
                        break;
                }
            }

            base.OnMzCommand(wParam, lParam);
        }
        /// <summary>
        /// 填充列表
        /// </summary>
        private void FillList()
        {
            WaitDialog.Begin(this);

            Thread.Sleep(1500);

            if(list.Items.Count>0)
            {
                list.Items.Clear();
            }

            for (int i = 0; i < 1200; i++)
            {
                ListItem item = new ListItem("测试数据_^&*#$%$#%<>?|+_" + (i + 1).ToString(), "俺是重绘字符串^&*#$%$#%<>?|+_！");

                list.Items.Add(item);
            }

            WaitDialog.End();
        }

        protected override void Dispose(bool disposing)
        {
            base.Dispose(disposing);
        }
    }
}
